﻿app.controller('ApplicationPagesController', ['$scope', 'GlobalService', 'fileUpload', 'ApplicationService', 'alertboxservice', function ($scope, GlobalService, fileUpload, ApplicationService, alertboxservice) {

    $scope.currentPage = 1;
    $scope.searchkey = "";
    $scope.sortmode = "DESC";
    $scope.sortby = "DEFAULT";
    $scope.showgridloader = true;
    $scope.viewmode = 'TABLE';
    $scope.perpage = 10;
    $scope.ChurchWorker = {};

    BindGrid();

    $scope.sort = function () {
        if ($scope.sortmode == 'ASC') {
            $scope.sortmode = 'DESC';
        }
        else {
            $scope.sortmode = 'ASC';
        }
        BindGrid();
    }

    $scope.Refresh = function (modelstate, id) {
        $scope.showgridloader = true;
        var url;
        url = "UpdateNewPages";
        ApplicationService.Getdata(url).success(function (result) {
            alertboxservice.ShowMsg(result, "Page ");
            $scope.showgridloader = false;
            BindGrid();
        });
    }

    $scope.toggle = function (modelstate, id) {
        url = "GetPage";
        ApplicationService.Getdata(url, id).success(function (data) {
            $scope.Pages = data;
        });
        $("#Page").modal({ backdrop: 'static' });
    }

    $scope.CurrentStatuss = [{ statusId: 1, statusName: "Active" }, { statusId: 2, statusName: "Depricated" }, { statusId: 3, statusName: "Deleted" }];

    function BindGrid() {
        $scope.showgridloader = true;
        url = "GetPages?PageNo=" + $scope.currentPage + "&SearchKey=" + $scope.searchkey + "&Sort=" + $scope.sortby + "&SortMode=" + $scope.sortmode + "&PerPage=" + $scope.perpage;
        ApplicationService.Getdata(url).success(function (data) {
            $scope.showgridloader = false;
            $scope.list = data;
            if (data.length > 0) {
                $scope.entryLimit = 10;
                $scope.filteredItems = data[0].TotalRecords;
                $scope.totalItems = data[0].TotalRecords;
            }
        });
    }

    $scope.setPage = function (pageNo) {
        $scope.currentPage = pageNo;
        BindGrid();
    };

    $scope.sort_by = function (predicate) {
        $scope.predicate = predicate;
        $scope.reverse = !$scope.reverse;
    };

    $scope.save = function (modelstate, id) {
        $scope.buttonstate = true;
        var url;
        url = "UpdatePage";
        $scope.sortmode = "DESC";
        $scope.sortby = "DEFAULT";
        ApplicationService.Save($scope.Pages, url).success(function (result) {
            alertboxservice.ShowMsg(result, "Page ");
            $scope.buttonstate = false;
            BindGrid();
        });
    }

}]);